<template>
  <div class="login_warp">
    <div class="top">
      <div class="top_title">半价尚品</div>
      <div class="top_title1">运营后台管理系统</div>
      <!-- <div class="top_img1"></div> -->
    </div>
    <div class="right">
      <div class="loginCon">
        <div class="login" @click="$refs.verify.show = false">
          <Row @keydown.enter.native="submitLogin" class="flex">
            <Col style="width: 368px">
              <p class="blue_text">登录</p>
              <Row style="flex-direction: column">
                <Form
                  ref="usernameLoginForm"
                  :model="form"
                  :rules="rules"
                  class="form"
                >
                  <FormItem prop="username">
                    <Input
                      v-model="form.username"
                      prefix="ios-contact"
                      size="large"
                      clearable
                      placeholder="请输入用户名"
                      autocomplete="off"
                    />
                  </FormItem>
                  <FormItem prop="password">
                    <Input
                      type="password"
                      v-model="form.password"
                      prefix="ios-lock"
                      size="large"
                      password
                      placeholder="请输入密码"
                      autocomplete="off"
                    />
                  </FormItem>
                </Form>

                <Row>
                  <Button
                    class="login-btn"
                    type="primary"
                    size="large"
                    :loading="loading"
                    @click="submitLogin"
                    long
                  >
                    <span v-if="!loading">{{ $t("login") }}</span>
                    <span v-else>{{ $t("logining") }}</span>
                  </Button>
                </Row>
              </Row>
              <!-- 拼图验证码 -->
              <verify
                ref="verify"
                class="verify-con"
                verifyType="LOGIN"
                @change="verifyChange"
              ></verify>
            </Col>
            <!-- <LangSwitch /> -->
          </Row>
        </div>
      </div>
      <div class="foot">
        <Row type="flex" justify="center" class="copyright">
          Copyright © {{ year }} - Present
          <a target="_self" style="margin: 0 5px">{{ config.title }}</a>
          版权所有
        </Row>
      </div>
    </div>
  </div>
</template>

<script>
import { login, userInfo } from "@/api/index";
import Cookies from "js-cookie";
import Header from "@/views/main-parts/header";
import Footer from "@/views/main-parts/footer";
import LangSwitch from "@/views/main-parts/lang-switch";
import util from "@/libs/util.js";
import verify from "@/components/verify";

export default {
  components: {
    LangSwitch,
    Header,
    Footer,
    verify,
  },
  data() {
    return {
      year: new Date().getFullYear(),
      config: require("@/config"),
      loading: false, // 加载状态
      form: {
        // 表单数据
        username: "",
        password: "",
        mobile: "",
        code: "",
      },
      rules: {
        // 验证规则
        username: [
          {
            required: true,
            message: "账号不能为空",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    afterLogin(res) {
      // 登录成功后处理
      let accessToken = res.result.accessToken;
      let refreshToken = res.result.refreshToken;
      this.setStore("accessToken", accessToken);
      this.setStore("refreshToken", refreshToken);
      // 获取用户信息
      userInfo().then((res) => {
        if (res.success) {
          // 加载菜单
          Cookies.set("userInfoManager", JSON.stringify(res.result));
          this.$store.commit("setAvatarPath", res.result.avatar);
          util.initRouter(this);
          this.$router.push({
            name: "home_index",
          });
        } else {
          this.loading = false;
        }
      });
    },
    submitLogin() {
      // 登录操作
      this.$refs.usernameLoginForm.validate((valid) => {
        if (valid) {
          this.$refs.verify.init();
        }
      });
    },
    verifyChange(con) {
      // 拼图验证码回显
      if (!con.status) return;

      this.loading = true;

      let fd = new FormData();
      fd.append("username", this.form.username);
      fd.append("password", this.md5(this.form.password));
      login(fd)
        .then((res) => {
          if (res && res.success) {
            this.afterLogin(res);
          } else {
            this.loading = false;
          }
        })
        .catch(() => {
          this.loading = false;
        });
      this.$refs.verify.show = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  // background: url("../assets/background.png") no-repeat;
  // background-color: #f0f2f5;
  // display: flex;
  // background-size: cover;
  // align-items: center;
  // justify-content: center;

  .verify-con {
    position: absolute;
    top: 150px;
    z-index: 10;
    left: 20px;
  }
  .form {
    padding-top: 1vh;
  }

  .login-btn {
    background: linear-gradient(135deg, $theme_color 0%, $warning_color 100%);
    height: 40px;
    cursor: pointer;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    color: #fff;
    width: 100%;
    text-align: center;
    transition: 0.35s;
  }
  .login-btn:hover {
    opacity: 0.9;
    border-radius: 10px;
  }
}
.flex {
  justify-content: center;
}
.login_warp {
  width: 100%;
  height: 100vh;
  background: #fff;
  padding: 40px;
  box-sizing: border-box;
  overflow: hidden;
  display: flex;

  .cur_p {
    cursor: pointer;
  }
  .top {
    width: 50%;
    height: 100%;
    background-color: linear-gradient(134deg, #ffe3e3 0%, #ff4c4c 100%);
    background-image: url("../assets/login/logo.png");
    background-repeat: no-repeat;
    background-size: 688px 424px;
    background-position: bottom;
    padding: 74px 116px 0 116px;
    box-sizing: border-box;
    .top_title {
      font-family: YouYuan;
      line-height: 81px;
      font-size: 48px;
      font-weight: bold;
      font-stretch: normal;
      letter-spacing: -4px;
      color: #ff4c4c;
      font-style: italic;
      cursor: pointer;
      margin-bottom: 16px;
    }
    .top_title1 {
      font-family: SourceHanSansCN, SourceHanSansCN;
      font-weight: bold;
      font-size: 46px;
      color: #333333;
      line-height: 69px;
      letter-spacing: 1px;
      text-align: left;
      font-style: normal;
      margin-bottom: 122px;
    }
    .top_img1 {
      width: 688px;
      height: 464px;
      background: url("../assets/login/logo.png") no-repeat;
      background-size: 100% 100%;
    }
  }
  .right {
    width: 50%;
    height: 100%;
    background: #fffafa;
    position: relative;
  }
  .top_text {
    width: 150px;
    height: 34.31px;
    color: #ff4c4c;
    font-family: Microsoft YaHei;
    font-size: 24px;
    font-weight: 700;
    line-height: 32px;
    letter-spacing: 0px;
    text-align: left;
  }
  .content_form {
    width: 460px;
    background: rgb(255, 255, 255);
    border-radius: 8px;
    margin: auto;
    padding: 20px;
    padding-bottom: 60px;
    box-sizing: border-box;
    box-shadow: 3.91px 5.8px 13px rgba(0, 29, 96, 0.1);

    .el-form /deep/ .el-input_inner {
      border: none;
      border-bottom: 1px solid #ddd;
      margin-bottom: 15px;
    }
  }
}

.login_btn {
  width: 100%;
  height: 51.47px;
  background: #ff4c4c;
  background-blend-mode: normal, normal;
  border-radius: 24px;
  display: block;
  margin: auto;
  margin-top: 40px;
  border: none;
  font-weight: 400;
  font-size: 16px;
  color: #ffffff;
}
.text {
  width: 339px;
  height: 70px;
  color: rgb(32, 100, 207);
  font-size: 33.89px;
  line-height: 50px;
  margin-left: 200px;
  margin-right: 136px;

  p {
    margin: 20px auto;
  }
  .muic {
    display: flex;
    div {
      width: 5px;
      height: 15px;
      background: rgb(32, 100, 207);
      margin-right: 8px;
      margin-top: 16px;
    }
    .line {
      height: 31px;
      margin-top: 0;
    }
  }
}
.el-input /deep/ .el-input__inner {
  border: none;
  border-bottom: 1px solid #ddd;
  margin-bottom: 15px;
}
/deep/ .el-form-item__error {
  color: #f56c6c;
  font-size: 12px;
  line-height: 1;
  padding-left: 15px;
  position: absolute;
  top: 100%;
  left: 0;
}
.inputDemo {
  position: relative;
  .flex_center {
    position: absolute;
    right: 0;
    top: -8px;
    color: #ff4c4c;
    font-family: MicrosoftYaHei;
    font-size: 13.85px;
    width: 78px;
    text-align: center;
  }
}
.loginCon {
  margin-top: 200px;
}
.blue_text {
  height: 32px;
  color: #ff4c4c;
  font-family: Microsoft YaHei;
  font-size: 36px;
  font-weight: 400;
  line-height: 32px;
  text-align: center;
  margin: auto;
  margin-bottom: 50px;
}
.foot {
  position: absolute;
  bottom: 8vh;
  width: 368px;
  left: calc(50% - 184px);
  color: rgba(0, 0, 0, 0.45);
  font-size: 14px;

  .help {
    margin: 0 auto;
    margin-bottom: 1vh;
    width: 60%;

    .item {
      color: rgba(0, 0, 0, 0.45);
    }

    :hover {
      color: rgba(0, 0, 0, 0.65);
    }
  }
}
</style>
